package org.redis.cluster.statistics;

import java.util.concurrent.atomic.AtomicInteger;

import org.redis.cluster.Ordered;
import org.redis.cluster.event.Event;
import org.redis.cluster.event.EventListener;
import org.redis.cluster.interceptor.Interceptor;
import org.redis.cluster.interceptor.Invocation;
import org.redis.cluster.redis.transport.RedisReply;
import org.redis.cluster.scheduler.second.SecondScheduleEvent;

public class CommandInvokeStatistics implements EventListener<SecondScheduleEvent>,Interceptor,Ordered {
	
	private final static AtomicInteger invokeCounter=new AtomicInteger();
	private final static AtomicInteger opsCounter=new AtomicInteger();
	
	@Override
	public RedisReply intercept(Invocation invocation) throws Throwable {
		invokeCounter.incrementAndGet();
		opsCounter.incrementAndGet();
		return invocation.invoke();
	}

	@Override
	public void onEvent(SecondScheduleEvent event) {
		opsCounter.set(0);
	}

	@Override
	public boolean isSupport(Event event) {
		return (event instanceof SecondScheduleEvent);
	}
	
	@Override
	public int getOrder() {
		return Ordered.MEDIUM;
	}
	
	public static int getInvokeCount() {
		return invokeCounter.get();
	}
	
	public static int getOpsCount() {
		return opsCounter.get();
	}
}